using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace 客户端{ public partial class Client : Form { public Client() { InitializeComponent(); } Socket socketSend; Thread th; bool IsAuto = false; string head = "500000FF03FF00";//报文头, string length = ""; string time = "0010"; string function_code = ""; string different_code = ""; string data = ""; string start = ""; string count = ""; private void btn_connect_Click(object sender, EventArgs e) { if (btn_connect.Text=="连接") { try { //创建负责通信的Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(txtServer.Text); IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text)); //获得要连接的远程服务器应用程序的IP和端口号 socketSend.Connect(point); ShowMsg("连接成功" socketSend.RemoteEndPoint); //开启一个新的线程接受服务器接受的数据 th = new Thread(Recive); th.IsBackground = true; th.Start(); btn_connect.Text = "关闭"; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } } else { Connect_Close(); ShowMsg("连接关闭"); btn_connect.Text = "连接"; timer1.Stop(); btnAutoSend.Text = "自动发送"; btnAutoSend.BackColor = Color.Green; IsAuto = false; } } void ShowMsg(string str) { string space = " "; string NewStr = string.Format("{0}{1}{2}", str, space,DateTime.Now.ToString()); textBox3.AppendText(NewStr "\r\n"); } private void Form1_Load(object sender, EventArgs e) { txtServer.Text= "192.168.1.4"; txtPort.Text = "4000"; Control.CheckForIllegalCrossThreadCalls = false; rbtnM_Read.Checked = true; txtMorDStart.Text = "100"; txtCount.Text = "1"; btn_send.Text = "读取"; } private void btn_send_Click(object sender, EventArgs e) { Send(); } private void Send() { try { if (socketSend.Connected) { string str = ""; if (rbtnM_Read.Checked) { str = M_read(str); } else if (rbtnM_Write.Checked) { str = M_Write(str); } else if (rbtnD_Read.Checked) { str = D_read(str); } else if (rbtnD_Write.Checked) { str = D_write(str); } byte[] buffer = System.Text.Encoding.ASCII.GetBytes(str); socketSend.Send(buffer); } } catch { } } private string M_read(string str) { function_code = "0401"; different_code = "0001"; data = "M*"; start = txtMorDStart.Text.PadLeft(6, '0'); int k = int.Parse(txtCount.Text); count = k.ToString("x").PadLeft(4, '0'); length = (time.Length function_code.Length different_code.Length data.Length start.Length count.Length).ToString("x4"); str = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}", head, length, time, function_code, different_code, data, start, count); return str; } private string M_Write(string str) { function_code = "1401"; different_code = "0001"; data = "M*"; start = txtMorDStart.Text.PadLeft(6, '0'); int k = int.Parse(txtCount.Text); count = k.ToString("x").PadLeft(4, '0'); length = (time.Length function_code.Length different_code.Length data.Length start.Length count.Length k).ToString("x4"); str = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}", head, length, time, function_code, different_code, data, start, count, txtValueWrite.Text.PadLeft(k, '0')); return str; } private string D_read(string str) { function_code = "0401"; different_code = "0000"; data = "D*"; start = txtMorDStart.Text.PadLeft(6, '0'); int k = int.Parse(txtCount.Text); count = k.ToString("x").PadLeft(4, '0'); length = (time.Length function_code.Length different_code.Length data.Length start.Length count.Length).ToString("x4"); str = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}", head, length, time, function_code, different_code, data, start, count); return str; } private string D_write(string str) { function_code = "1401"; different_code = "0000"; data = "D*"; start = txtMorDStart.Text.PadLeft(6, '0'); int k = int.Parse(txtCount.Text); count = k.ToString("x").PadLeft(4, '0').ToUpper(); length = (time.Length function_code.Length different_code.Length data.Length start.Length count.Length k * 4).ToString("x4").ToUpper(); string sumend = ""; string sumtemp = ""; int k1 = txtValueWrite.Text.Length; string sum = txtValueWrite.Text; int DifferenceValue = 4 * k - k1; if (DifferenceValue==0) { sumend = sum; } else if (DifferenceValue>0) { sumend = sum.PadLeft(4 * k, '0'); //PadRight(4 * k, '0'); } else { } str = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}", head, length, time, function_code, different_code, data, start, count, sumend); return str; } void Recive() { while (true) { try { //客户端连接成功后,服务器接受客户端发来的信息 byte[] buffer = new byte[1024 * 1024 * 2]; //实际接受的有效字节数--r int r = socketSend.Receive(buffer); if (r == 0) { break; } string str = Encoding.ASCII.GetString(buffer, 0, r); if (r > 22) { txtValueRead.Text = str.Remove(0, 22); } ShowMsg(socketSend.RemoteEndPoint ":" str); } catch { } } } private void Client_FormClosing(object sender, FormClosingEventArgs e) { Connect_Close(); } private void Connect_Close() { if (socketSend != null) { if (socketSend.Connected) { socketSend.Close(); } } if (th != null) { if (th.IsAlive) { th.Abort(); } } } private void btnAutoSend_Click(object sender, EventArgs e) { if (!IsAuto) { timer1.Start(); btnAutoSend.Text = "关闭"; btnAutoSend.BackColor = Color.Red; IsAuto = true; rbtnM_Read.Enabled = false; rbtnD_Write.Enabled = false; rbtnD_Read.Enabled = false; rbtnM_Write.Enabled = false; txtMorDStart.Enabled = false; txtCount.Enabled = false; txtValueWrite.Enabled = false; } else { timer1.Stop(); btnAutoSend.Text = "自动发送"; btnAutoSend.BackColor = Color.Green; IsAuto = false; rbtnM_Read.Enabled = true; rbtnD_Write.Enabled = true; rbtnD_Read.Enabled = true; rbtnM_Write.Enabled = true; txtMorDStart.Enabled = true; txtCount.Enabled = true; txtValueWrite.Enabled = true; } } private void timer1_Tick(object sender, EventArgs e) { Send(); } private void timer2_Tick(object sender, EventArgs e) { if (rbtnM_Read.Checked) { btn_send.Text = "M读取"; if (btnAutoSend.Text!="关闭") { btnAutoSend.Text = "M自动读取"; } } else if (rbtnM_Write.Checked) {btn_send.Text = "M写入"; if (btnAutoSend.Text != "关闭") { btnAutoSend.Text = "M自动写入"; } } else if (rbtnD_Read.Checked) { btn_send.Text = "D读取"; if (btnAutoSend.Text != "关闭") { btnAutoSend.Text = "D自动读取"; } } else if (rbtnD_Write.Checked) { btn_send.Text = "D写入"; if (btnAutoSend.Text != "关闭") { btnAutoSend.Text = "D自动写入"; } } } }}
下载三菱以太网3E帧ASCII通信实例源码(三菱Q系列MC协议)用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论